<template>
  <div class="register">
    <div class="bac-dialog">
      <div class="back-ground">
        <div class="bac-title">
          线上教学课堂
        </div>
        <div class="bac-img">
          <img src="../../assets/classroom-register-bac.png" alt="">
        </div>
      </div>
      <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form">
        <h3 class="title">用户注册</h3>
        <el-form-item prop="account">
          <el-input v-model="registerForm.account" type="text" auto-complete="off" placeholder="账号(手机号)">
            <svg-icon slot="prefix" icon-class="phone" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="realName">
          <el-input v-model="registerForm.realName" type="text" auto-complete="off" placeholder="用户名">
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="registerForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleRegister"
            @blur="qryPwdRule(registerForm.password)" 
            @focus="focusReset"
            >
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            auto-complete="off"
            placeholder="确认密码"
            @keyup.enter.native="handleRegister"
          >
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaOnOff">
          <el-input
            v-model="registerForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 67%"
            @keyup.enter.native="handleRegister"
          >
            <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
          </el-input>
          <div class="register-code">
            <el-link class="register-code-img" style="color: #007aff;" >获取验证码</el-link>
            <!-- <img :src="codeUrl" @click="getCode" class="register-code-img"/> -->
          </div>
        </el-form-item>
        <el-form-item style="width:100%;">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="height: 42px;width:100%;background: #2d6eff;"
            @click.native.prevent="handleRegister"
          >
            <span v-if="!loading">注 册</span>
            <span v-else>注 册 中...</span>
          </el-button>
          <div style="float: right;">
            <router-link class="link-type" style="color: #007aff;" :to="'/classroomLogin'">使用已有账户登录</router-link>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// import { getCodeImg, register } from "@/api/login";
import { onlineUserRegister,onlineQryPwdRule } from '@/framework/api/user'
import { encryption } from 'DQBasic-vue-component'

export default {
  name: "Register",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      codeUrl: "",
      registerForm: {
        realName: "",
        password: "",
        confirmPassword: "",
        code: "",
        account: "",
        uuid: "",
        sourceType:"2"
      },
      registerRules: {
        account: [
          { required: false, pattern: /^1[34578]\d{9}$/, message: '请输入正确的手机号码', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
          //{ min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' },
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请再次输入您的密码" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ]
        // ,code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      captchaOnOff: true
    };
  },
  created() {
    // this.getCode();
  },
  methods: {
    focusReset() {
      this.registerRules.password.pop()
      this.$refs.registerForm.clearValidate('password')
    },
    qryPwdRule(data) {
      onlineQryPwdRule(data).then(res => {
      }).catch((err) => {
        const message = String(err).split(': ')[1]
        this.registerRules.password.push({
          validator: (rule, value, callback) => {
              callback(new Error(message))
            },
            trigger: 'blur'
        })
        this.$nextTick(() => {
          this.$refs.registerForm.validateField('password')
        })
      })
      
      /**{
            validator: (rule, value, callback) => {
              if ("00000"== this.qryPwdRule(this.registerForm.password).code) {
                callback(new Error(this.qryPwdRule(this.registerForm.password).message))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          } */
    },
    getCode() {
      // getCodeImg().then(res => {
      //   this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
      //   if (this.captchaOnOff) {
      //     this.codeUrl = "data:image/gif;base64," + res.img;
      //     this.registerForm.uuid = res.uuid;
      //   }
      // });
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          const data = encryption({ data: this.registerForm })
          onlineUserRegister(data).then(res => {
            const account = this.registerForm.account;
            this.$alert("<font color='red'>恭喜你，您的账号 " + account + " 注册成功！</font>", '系统提示', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              this.$router.push("/login");
            }).catch(() => {});
          }).catch(() => {
            this.loading = false;
            if (this.captchaOnOff) {
              this.getCode();
            }
          })
        }
      });
    },
    register(mydata) { 
      console.log("进入了register方法");
      onlineUserRegister(mydata);
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.register {
  height: 100%;
  display: flex;
  background-image: url("../../assets/classroom-bac.png");
  background-size: cover;
  .bac-dialog {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: auto;
    width: 1400px;
    height: 812px;
    border-radius: 8px;
    background: #fff;
  }
}

.back-ground {
  margin-right: 266px;
  .bac-title {
    font-family: "YouSheBiaoTiHei";
    font-weight: 400;
    font-size: 42px;
    text-align: left;
    color: #004fff;
    position: absolute;
    top: 100px;
    left: 325px;
  }
  .bac-img {
    width: 530.24px;
    height: 442.41px;
    background: transparent;
  }
}
.register .title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
  border: 2px ;
  border-bottom: 3px solid #004fff ;
  width: 76px;
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.register-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.register-code-img {
  margin-left: 30px;
  height: 38px;
}
</style>
